package com.humh.library.model;



import java.util.Arrays;
import java.util.List;

public class LibrarySystemTestWithRules {



 private static LibraryContext libraryContext  ;

  public static void initData() {
    // 创建库存管理实例
    InventoryManagement inventoryManagement=new InventoryManagement();
    // 创建图书馆管理员
     Librarian librarian = new Librarian("L001", "John Doe", inventoryManagement);

    libraryContext = new LibraryContext(inventoryManagement, librarian);

    // 创建图书
    Book book1 = new Book("978-3-16-148410-0", "Effective Java", "Joshua Bloch");
    Book book2 = new Book("978-1-4919-1889-0", "Clean Code", "Robert C. Martin");
    Book book3 = new Book("978-0-321-35668-0", "Design Patterns", "Erich Gamma");
    Book book4 = new Book("978-0-134-45468-8", "Refactoring", "Martin Fowler");
    Book book5 = new Book("978-0-596-51774-8", "The Pragmatic Programmer", "Andrew Hunt");
    Book book6 = new Book("978-0-131-35084-2", "Java Concurrency in Practice", "Brian Goetz");

    // 增加图书库存
    librarian.maintainCatalog(book1, 5);
    librarian.maintainCatalog(book2, 3);
    librarian.maintainCatalog(book3, 2);
    librarian.maintainCatalog(book4, 4);
    librarian.maintainCatalog(book5, 6);
    librarian.maintainCatalog(book6, 2);

    // 创建读者
    Reader reader = new Reader("U001", "Alice", "alice@example.com");
    libraryContext.addReader(reader);

  }

  public static void main(String[] args) {

    initData();

    InventoryManagement inventoryManagement = libraryContext.getInventoryManagement();


    // 创建用户
    Reader reader1 = libraryContext.getReaderById("U001");

    Book book1 = inventoryManagement.findBookByISBN("978-3-16-148410-0");
    Book book2 = inventoryManagement.findBookByISBN("978-1-4919-1889-0");
    Book book3 = inventoryManagement.findBookByISBN("978-0-321-35668-0");
    Book book4 = inventoryManagement.findBookByISBN("978-0-134-45468-8");
    Book book5 = inventoryManagement.findBookByISBN("978-0-596-51774-8");
    Book book6 = inventoryManagement.findBookByISBN("978-0-131-35084-2");

    // 用户尝试借书，测试借书规则
    List<Book> booksToBorrow1 = Arrays.asList(book1, book2, book3, book4, book5);
    Librarian librarian = libraryContext.getLibrarian();
    librarian.manageBorrowing(reader1, booksToBorrow1);

    // 再次尝试借书，超出限制的情况下无法借阅
    System.out.println("\nTesting user borrowing more than 5 books:");
    List<Book> booksToBorrow2 = Arrays.asList(book6);
    librarian.manageBorrowing(reader1, booksToBorrow2); // 用户已经借了5本书，不能再借

    // 用户归还部分书籍并尝试再借书
    System.out.println("\nTesting returning books and borrowing more:");
    List<Book> booksToReturn = Arrays.asList(book1, book2);
    librarian.manageReturning(reader1, booksToReturn); // 归还部分书籍

    // 归还后再借书
    librarian.manageBorrowing(reader1, booksToBorrow2); // 现在可以借书

    // 测试归还超期书籍的规则
    System.out.println("\nTesting returning overdue books:");
    LoanRecord loanRecord = reader1.getLoanRecords().get(0);
    loanRecord.setReturnDate(loanRecord.getLoanDate().minusDays(10)); // 模拟超期

    // 用户归还超期书籍
    librarian.manageReturning(reader1, Arrays.asList(book1)); // 归还书籍，超期产生罚款

    // 用户支付罚金并归还书籍
    reader1.payFine(100); // 假设用户支付了罚金
    librarian.manageReturning(reader1, Arrays.asList(book3)); // 完成归还
  }
}
